package org.jetnuke.dao.generic;

import org.apache.log4j.Logger;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.PollData;

public class GenericPollDataDAO implements org.jetnuke.dao.PollDataDAO {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(GenericPollDataDAO.class);

	private List processSelectAll(ResultSet rs) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - start"); //$NON-NLS-1$
		}

		List list = new ArrayList();
		while (rs.next()) {
			PollData u = new PollData();
			fillPollDataFromResultSet(u, rs);
			list.add(u);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/**
	 * @param u
	 * @param rs
	 */
	private void fillPollDataFromResultSet(PollData u, ResultSet rs)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("fillPollDataFromResultSet(PollData, ResultSet) - start"); //$NON-NLS-1$
		}

		u.setOPTIONCOUNT(rs.getInt("OPTIONCOUNT"));
		u.setOPTIONTEXT(rs.getString("OPTIONTEXT"));
		u.setPOLLID(rs.getInt("POLLID"));
		u.setVOTEID(rs.getInt("VOTEID"));

		if (logger.isDebugEnabled()) {
			logger
					.debug("fillPollDataFromResultSet(PollData, ResultSet) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.PollDataModel#selectByPollIDOrderVoteID(int)
	 */
	public List selectByPollIDOrderVoteID(int pollID) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectByPollIDOrderVoteID(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext
				.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("PollDataModel.selectByPollIDOrderVoteID"));
		p.setInt(1, pollID);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectByPollIDOrderVoteID(int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.PollDataModel#retrieveByPK(int, int)
	 */
	public PollData retrieveByPK(int pollID, int voteID) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int, int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("PollDataModel.selectByID"));
		p.setInt(1, pollID);
		p.setInt(2, voteID);

		ResultSet rs = p.executeQuery();
		PollData s = new PollData();

		if (rs.next()) {
			fillPollDataFromResultSet(s, rs);
		}

		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int, int) - end"); //$NON-NLS-1$
		}
		return s;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.PollDataModel#update(org.jetnuke.entities.PollData)
	 */
	public void update(PollData dt) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("update(PollData) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(SystemGlobals.getSql("PollDataModel.update"));
		p.setInt(1, dt.getOPTIONCOUNT());
		p.setString(2, dt.getOPTIONTEXT());
		p.setInt(3, dt.getPOLLID());
		p.setInt(4, dt.getVOTEID());
		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("update(PollData) - end"); //$NON-NLS-1$
		}
	}

}
